package manager;

import java.util.Date;
import java.util.List;

import model.Profile;
import model.User;
import dao.UserDAO;

/**
 * Managing User, using singleton
 * @author xbingo
 *
 */
public class UserManager {
	private static UserDAO userDao = new UserDAO();
	private static UserManager userManager = new UserManager();
	
	private UserManager(){
		
	}
	
	/**
	 * 
	 * @return UserManager instance
	 */
	public static UserManager getUserManager() {
		return userManager;
	}
	
	public void register(User user) throws UserAlreadyExistsException{
		List<User> list = userDao.findByUsername(user.getUsername());
		if(!list.isEmpty()){
			throw new UserAlreadyExistsException();
		}else {
			userDao.getSession().beginTransaction();
			user.setProfile(new Profile());
			user.setRegisterDate(new Date());
			userDao.save(user);
			userDao.getSession().getTransaction().commit();
		}
	}
	
	
}
